package com.dcjt.firehotcarpad.widget

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import com.aries.library.fast.util.ToastUtil
import com.dcjt.firecommand.entity.RecordData
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.utils.ExoPlayer
import com.google.android.exoplayer2.ExoPlaybackException
import com.lxj.xpopup.impl.FullScreenPopupView
import kotlinx.android.synthetic.main.pop_play_layout.view.*

@SuppressLint("ViewConstructor")
class RecordPopupWindow(context: Context, private val isSingle: Boolean = false) :
    FullScreenPopupView(context),
    ExoPlayer.AudioControlListener {

    override fun getImplLayoutId() = R.layout.pop_play_layout

    var position = 0
    val data: MutableList<RecordData> = mutableListOf()

//    val data = mutableListOf(MusicBean("SW-休闲FM-Tag-02","http://image.kaolafm.net/mz/mp3_64/201411/2f4b3e7f-10c0-4aec-8be1-6ceb61c99805.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000758962"),
//        MusicBean("你怎么舍得我难过","http://image.kaolafm.net/mz/mp3_64/201402/ef60c2c3-2ed0-4321-84c4-0296cb52a2ac.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000002083"),
//        MusicBean("伤心对我来说","http://image.kaolafm.net/mz/mp3_64/201401/eb0a9e51-1cbe-463e-8b42-9ec6b85df50a.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000001692"),
//        MusicBean("Smoke Gets In Your Eyes","http://image.kaolafm.net/mz/mp3_64/201404/f9422dbf-ac04-4f8e-bb04-664422403cc0.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000019653"),
//        MusicBean("晚安","http://image.kaolafm.net/mz/mp3_64/201403/0875d88d-bc3c-491f-80be-5d3aea82aa21.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000017851"),
//        MusicBean("考拉FM-SW08","http://image.kaolafm.net/mz/mp3_64/201404/43b1835c-50ff-46c2-bf90-a9e4da8ca4e6.mp3?appid=fiaju0644&deviceid=100000029020744&audioid=1000000175600")
//    )

    private var exoPlayer: ExoPlayer? = null


    @SuppressLint("SetTextI18n")
    override fun initPopupContent() {
        super.initPopupContent()
        if (data.isNotEmpty()) initMusic(position)

        iv_play.setOnClickListener {
            if (exoPlayer?.isPlay() == true) exoPlayer?.onPause()
            else exoPlayer?.onStart(position)
        }

        if (isSingle) {
            iv_previous.visibility = View.GONE
            iv_next.visibility = View.GONE
        } else {
            iv_previous.setOnClickListener {
                initMusic(--position)
                tv_length.text = "00:00"
                tv_current.text = "00:00"
                progress_bar.setProgress(0)

            }
            iv_next.setOnClickListener {
                initMusic(++position)
                tv_length.text = "00:00"
                tv_current.text = "00:00"
                progress_bar.setProgress(0)

            }
        }


        iv_close.setOnClickListener {
            exoPlayer?.release()
            dismiss()
        }
    }


    @SuppressLint("SetTextI18n")
    private fun initMusic(index: Int) {

        iv_previous.isEnabled = index != 0
        iv_next.isEnabled = index + 1 != data.size

        tv_name.text = data[index].name
        tv_name.isSelected = true
        exoPlayer = ExoPlayer(context)
        exoPlayer?.listener = this

        exoPlayer?.onPrepare(data[index].url)
        exoPlayer?.onStart(index)
    }


    @SuppressLint("CheckResult")
    fun initData(data: MutableList<RecordData>, index: Int = 0) {
        this.data.addAll(data)
        this.position = index
        exoPlayer?.onStart(position)
    }

    override fun setCurPositionTime(position: Int, curPositionTime: Long) {
//        EasyLog.DEFAULT.e("curPositionTime--$curPositionTime")
//        isPlay(position,curPositionTime.toInt() != progress_bar.rate)
        progress_bar.setProgress(curPositionTime.toInt())

    }

    override fun setDurationTime(position: Int, durationTime: Long) {
//        EasyLog.DEFAULT.e("durationTime--$durationTime")
        if (durationTime != Long.MAX_VALUE) {
            progress_bar.rate = durationTime.toInt()
        }
    }

    override fun setBufferedPositionTime(position: Int, bufferedPosition: Long) {

    }

    override fun setCurTimeString(position: Int, curTimeString: String) {
        tv_current.text = curTimeString
    }

    override fun isPlay(position: Int, isPlay: Boolean) {

        if (isPlay) {
            iv_play.setImageResource(R.drawable.ic_play_pause)
        } else {
            iv_play.setImageResource(R.drawable.ic_play_play)
        }
    }

    override fun setDurationTimeString(position: Int, durationTimeString: String) {
        tv_length.text = durationTimeString
    }

    override fun onPlayError(e: ExoPlaybackException) {
        dismiss()
        ToastUtil.show("音频播放失败")
    }


}

